package com.shanghaionstar.discover.presenter;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.mag.publicService.response.ResponBannerEntity;
import com.shanghaionstar.discover.iview.IDiscoverView;
import com.shanghaionstar.discover.service.DiscoverService;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.utils.BannerUtil;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/7/19.
 */
public class DiscoverFragmentPresenter extends BasePresenter<Class<?>> {
    private BaseActivity mContext;
    private IDiscoverView mDiscoverView;
    private DiscoverService mService;
    private List<ResponBannerEntity> entities;


    public DiscoverFragmentPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Class<?>> view) {
        super.setView(view);
        mDiscoverView = (IDiscoverView) view;
        mContext = (BaseActivity) mDiscoverView.onActivity();
        mService = new DiscoverService(mContext);
    }

//    public void commandData() {
//        subscription = service.commandData().subscribeOn(mvpScheduler.backgroundThread())
//                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<List<ResponBannerEntity>>() {
//                    @Override
//                    public void call(List<ResponBannerEntity> myDiscountResps) {
//                        if(myDiscountResps == null || myDiscountResps.isEmpty()){
//                            homeView.onShowBanner(null);
//                        }else{
//                            homeView.onShowBanner(myDiscountResps);
//                        }
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//                        //抛出Error异常
//                        homeView.onShowBanner(null);
//                    }
//                });
//    }

//    public void onBannerClick(ResponBannerEntity entity) {
//        Intent intent = new Intent(mContext, BannerActivity.class);
//        if (entity != null) {
//            if (!CommonUtil.isNull(entity.getShowType())
//                    && entity.getShowType().equals("2")) {
//                intent.putExtra("uri", entity.getUrl());
//                intent.putExtra("showType", entity.getShowType());
//                String isScaling = entity.getIsScaling();
//                intent.putExtra("isScaling", isScaling == null ? "" : isScaling);
//            } else {
//                intent.putExtra("uri", entity.getContentUrl());
//            }
//            String paramData = entity.getParamData();
//            if(!TextUtils.isEmpty(paramData)){
//                String h5Url = intent.getStringExtra("uri");
//                h5Url += "data="+paramData;
//                intent.putExtra("uri",h5Url);
//            }
//            intent.putExtra("receiveType", BannerLayout.BANNER_TAG);
//            intent.putExtra("title", entity.getTitle());
//            intent.putExtra("isH5Title", entity.getIsH5Title());
//        }
//        mContext.startActivity(intent);
//    }


    public void onBannerClick(ResponBannerEntity entity){
        BannerUtil util =  new BannerUtil(mContext);
        util.bannerClick(entity,mContext, FunctionNameFindId.UnSend,mvpScheduler,mDiscoverView);
    }

    public void commandData() {
        BannerUtil util = new BannerUtil(mContext);
        subscription = util.commandDataList(CategoryType.DISCOVERY_BANNER.value()).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<ResponBannerEntity>>() {
                    @Override
                    public void call(List<ResponBannerEntity> response) {
                        if(mContext == null)
                            return;
                        if (response != null && response.size() > 0) {
                            mDiscoverView.onGetBanner(response);
                        } else {
                            mDiscoverView.onGetBanner(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
//                        mDiscoverView.setLoading(false);
                        if(mContext == null)
                            return;
                        if (throwable != null && mDiscoverView != null)
                            mDiscoverView.onGetBanner(null);
                    }
                });
    }
}
